/**
  ******************************************************************************
  * @file    SCHEDULE_START.c 
  * @author  Feirui_Joe
  * @version V0.1
  * @date    07/26/2010
  * @brief   Display interface
  ******************************************************************************
  * @copy
  *
  * <h2><center>&copy; COPYRIGHT 2010 Feirui</center></h2>
  */ 



/**
  * @brief  INCLUDE FILES.
  */
#ifndef	SCHEDULE_START_GLOBALS
#define	SCHEDULE_START_GLOBALS
#include <includes.h>



/**
  * @brief  Global RAM Reset for Maintenance
  * @param  none
  * @return none.
  * @Caller POR
  * @Note   none.
  */
void PORGlobalRAMReset_Schedule(void)
{
  ScheduleRunningTimeCnt =0;
	
	
}


static uint8_t ScheduleSingleStartHandle(uint16_t TempCycleOpt,uint8_t ScheduleOrd ,uint8_t *Clock);

/* Handle Schedule */
void ScheduleStartWorkLoadHandle(void)
{
	if(GetFlagShareBufferDataContent(App_Sem_INPUTTOUCH,CfgOperaType_OSSem,ShareBuffer_InputTouchFlag,InputTouchFlag_InhibitScheduled))
	  return;
	if(GetDataBaseDataContent(AdrScheduled_StartOnOpt)==PcfgControlOpt_Disable)
	  return;
	
	uint8_t ActInform=DEF_FALSE;
	uint8_t TempClock[ClockOrd_TOTNum];
	ExchangeShareBufferDataContent(App_Sem_RTC,CfgOperaType_OSSem,TempClock,ShareBuffer_Clock,ClockOrd_TOTNum);
	uint16_t TempCycleOpt=GetDataBaseDataContent(AdrScheduled_StartOnCycleSet);
	/* Second shall be Zero */
	if(TempClock[ClockOrd_Sec]!=0)
	  return;
	
	ActInform|=ScheduleSingleStartHandle(TempCycleOpt,Schedule1Order ,TempClock);
	ActInform|=ScheduleSingleStartHandle(TempCycleOpt,Schedule2Order ,TempClock);
	ActInform|=ScheduleSingleStartHandle(TempCycleOpt,Schedule3Order ,TempClock);
	ActInform|=ScheduleSingleStartHandle(TempCycleOpt,Schedule4Order ,TempClock);
	ActInform|=ScheduleSingleStartHandle(TempCycleOpt,Schedule5Order ,TempClock);
	ActInform|=ScheduleSingleStartHandle(TempCycleOpt,Schedule6Order ,TempClock);
	ActInform|=ScheduleSingleStartHandle(TempCycleOpt,Schedule7Order ,TempClock);
	ActInform|=ScheduleSingleStartHandle(TempCycleOpt,Schedule8Order ,TempClock);
	ActInform|=ScheduleSingleStartHandle(TempCycleOpt,Schedule9Order ,TempClock);
	ActInform|=ScheduleSingleStartHandle(TempCycleOpt,Schedule10Order,TempClock);
	ActInform|=ScheduleSingleStartHandle(TempCycleOpt,Schedule11Order,TempClock);
	ActInform|=ScheduleSingleStartHandle(TempCycleOpt,Schedule12Order,TempClock);
	ActInform|=ScheduleSingleStartHandle(TempCycleOpt,Schedule13Order,TempClock);
	ActInform|=ScheduleSingleStartHandle(TempCycleOpt,Schedule14Order,TempClock);
	ActInform|=ScheduleSingleStartHandle(TempCycleOpt,Schedule15Order,TempClock);
	ActInform|=ScheduleSingleStartHandle(TempCycleOpt,Schedule16Order,TempClock);
	
	if(ActInform)
	  SetOSFlagsForNewInform(App_Flags_WORKFLOW,OSWORKFLOWFlags_ScheduleRequest);
}




static uint8_t ScheduleSingleStartHandle(uint16_t TempCycleOpt,uint8_t ScheduleOrd ,uint8_t *Clock)
{
	/* Schedule is running */
	uint32_t TempTiCnt;
	ExchangeShareBufferDataContent(App_Sem_RTC,CfgOperaType_OSSem,(uint8_t *)&TempTiCnt,(uint8_t *)&ScheduleRunningTimeCnt,4);
	if(TempTiCnt) return(DEF_FALSE);
	
	uint16_t ScheduleCfgBuffer[ScheduleAdrOrd_TOTNum];
	ReadDataBaseDataContent(ScheduleDataBaseAdrListTbl[ScheduleOrd],ScheduleCfgBuffer,ScheduleAdrOrd_TOTNum);
	
	if(ScheduleCfgBuffer[ScheduleAdrOrd_RunTime]==0)
	  return(DEF_FALSE);
	
	if(TempCycleOpt==ScheduleCycleOpt_Monthly)
	{
		if(Clock[ClockOrd_MonthWeek]!=(uint8_t)ScheduleCfgBuffer[ScheduleAdrOrd_RunWeekNum])
			return(DEF_FALSE);
	  if(Clock[ClockOrd_WeekOrd]!=(uint8_t)ScheduleCfgBuffer[ScheduleAdrOrd_RunWeekOrd])
			return(DEF_FALSE);
	}
	else if(TempCycleOpt==ScheduleCycleOpt_Weekly)
	{
		if(Clock[ClockOrd_WeekOrd]!=(uint8_t)ScheduleCfgBuffer[ScheduleAdrOrd_RunWeekOrd])
			return(DEF_FALSE);
	}
	
	/* Hour */
	if(Clock[ClockOrd_Hour]!=(uint8_t)(ScheduleCfgBuffer[ScheduleAdrOrd_RunClock]>>8))
	  return(DEF_FALSE);
	/* Min */
	if(Clock[ClockOrd_Min]!=(uint8_t)  ScheduleCfgBuffer[ScheduleAdrOrd_RunClock])
	  return(DEF_FALSE);
	
	TempTiCnt=(uint32_t)ScheduleCfgBuffer[ScheduleAdrOrd_RunTime]*60*WorkFlowTimingFreq;
	ExchangeShareBufferDataContent(App_Sem_RTC,CfgOperaType_OSSem,(uint8_t *)&ScheduleRunningTimeCnt,(uint8_t *)&TempTiCnt,4);
	
	return(DEF_TRUE);
}






/* Schedule Address List */
const uint16_t ScheduleDataBaseAdrListTbl[ScheduleOrdNum]={
  AdrScheduled_Start1RunWeekNum ,
  AdrScheduled_Start2RunWeekNum ,
  AdrScheduled_Start3RunWeekNum ,
  AdrScheduled_Start4RunWeekNum ,
  AdrScheduled_Start5RunWeekNum ,
  AdrScheduled_Start6RunWeekNum ,
  AdrScheduled_Start7RunWeekNum ,
  AdrScheduled_Start8RunWeekNum ,
  AdrScheduled_Start9RunWeekNum ,
  AdrScheduled_Start10RunWeekNum,
  AdrScheduled_Start11RunWeekNum,
  AdrScheduled_Start12RunWeekNum,
  AdrScheduled_Start13RunWeekNum,
  AdrScheduled_Start14RunWeekNum,
  AdrScheduled_Start15RunWeekNum,
  AdrScheduled_Start16RunWeekNum,
};



/*$PAGE*/#endif	/*	APP_GLOBALS_HCI	*/
